<?php

function experts_work_add_page($id) {
    return drupal_get_form('experts_work_edit_form',$id);
}

function experts_work_edit_page($id) {
    return drupal_get_form('experts_work_edit_form', $id);
}

function experts_work_edit_form($form, &$form_state) {
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    if (!empty($id)) {
        $record = nonghua_data_load('experts_work', 'experts_work_id', $id);
        if (empty($record)) {
            drupal_set_message(t('专家记录不存在！'), 'status', FALSE);
            return $form;
        }
    }
    if (!empty($_GET['expert_id'])) {
        $form_state['expert_id'] = $_GET['expert_id'];
    }
    if (!empty($form_state['expert_id'])) {
        $form['#expert_id'] = $form_state['expert_id'];
        $record->experts_id = $form['#expert_id'];
    }
    // 判断用户角色
    if(!nonghua_user_access('experts_work', 'view', $id, !empty($record) ? $record->author_uid:0, 0) ){
        drupal_set_message(t('您没有权限查看本条数据！'), 'status', FALSE);
        return $form;
    }
    $form['title'] = array(
        '#type' => 'textfield',
        '#title' => '标题',
        '#default_value' => !empty($record) ? $record->title : '',
        '#maxlength' => 50,
        '#required' => true
    );
    $form['content'] = array(
        '#type' => 'textarea',
        '#title' => '内容',
        '#default_value' => !empty($record) ? $record->content : '',
        '#maxlength' => 200,
        '#required' => true,
        '#attributes'=>array('placeholder'=>'最多输入200字。')
    );
    if(!empty($record->experts_id)){
        $experts_name = db_select('experts','E')
            ->condition('E.experts_id',$record->experts_id)
            ->fields('E')
            ->execute()
            ->fetchObject();
    }
    $role = user_access('add experts');
    $form['experts_name'] = array(
        '#type' => 'textfield',
        '#title' => '参与专家',
        '#default_value' => !empty($experts_name) ? $experts_name->experts_name : '',
        '#id'=>'search-expert-name',
        '#description'=> $role ? '<a  href="'.base_path().'experts/add"  class="btn btn-primary add-plant" target="_blank">添加专家</a>' : '',
        '#maxlength' => 50,
        '#required' => true,
    );
    $form['attendees'] = array(
        '#type' => 'textarea',
        '#title' => '参与人',
        '#default_value' => !empty($record) ? $record->attendees : '',
        '#maxlength' => 200,
        '#required' => true,
        '#attributes'=>array('placeholder'=>'最多输入200字。')
    );
    $form['work_date'] = array(
        '#type' => 'metercomm_date',
        '#title' => '日期',
        '#default_value' =>_date_db_2_form(!empty($record) ? $record->work_date : ''),
        '#size' => 32,
    );
    $form['address'] = array(
        '#type' => 'textfield',
        '#title' => '地点',
        '#default_value' => !empty($record) ? $record->address : '',
        '#maxlength' => 50,
        '#required' => true
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => arg(1) != 'add' ? '修改' : '添加',
        '#access' => nonghua_user_access('experts_work', 'edit', $id, !empty($record) ? $record->author_uid:0, 0)
    );
    return $form;
}

function experts_work_edit_form_validate($form, &$form_state) {
    if(empty($form_state['values']['experts_name'])){
        form_set_error('experts_name', '专家姓名不能为空');
    }else{
        $res = load_experts($form_state['values']['experts_name']);
       if(empty($res)){
           form_set_error('experts_name', '专家不存在，请选择其他专家');
       }
    }
}

function experts_work_edit_form_submit($form, &$form_state) {
    global $user;
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    if(!empty($form_state['values']['experts_name'])){
        $expert_info = load_experts($form_state['values']['experts_name']);
    }
    if (arg(1) == 'add') { //add new experts_work
        $nid = db_insert('experts_work') // Table name no longer needs {}
                ->fields(array(
                    'title' => $form_state['values']['title'],
                    'content' => $form_state['values']['content'],
                    'attendees' => $form_state['values']['attendees'],
                    'work_date' => _date_form_2_db($form_state['values']['work_date']),
                    'address' => $form_state['values']['address'],
                    'experts_id'=>$expert_info->experts_id,
                    'author_uid' => $user->uid,
                    'timestamp' => time(),))
                ->execute();
        if ($nid > 0) {
            drupal_set_message(t('添加成功！'), 'status', FALSE);
            drupal_goto('experts_work/add/'.$nid,array('query' => array('expert_id' =>$expert_info->experts_id)));
        }

    } else {
        $nid = db_update('experts_work')// edit exsting store. todo: update only modified fields
        ->fields(array(
            'title' => $form_state['values']['title'],
            'content' => $form_state['values']['content'],
            'attendees' => $form_state['values']['attendees'],
            'work_date' => _date_form_2_db($form_state['values']['work_date']),
            'address' => $form_state['values']['address'],
            'experts_id'=>$expert_info->experts_id,
//            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->condition('experts_work_id', $id, '=')
            ->execute();
        if ($nid > 0) {
            drupal_set_message(t('修改成功！'), 'status', FALSE);
        }

    }
}


function load_experts($expert_name){
    $res = db_select('experts','E')->condition('E.experts_name',$expert_name)->fields('E')->execute()->fetchObject();
    return $res;
}
